package com.ldn.server.mqtt.handler;

import com.ldn.server.mqtt.covert.MqttCovert;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.dromara.mica.mqtt.codec.MqttQoS;
import org.dromara.mica.mqtt.core.server.auth.IMqttServerSubscribeValidator;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.tio.core.ChannelContext;

/**
 * 订阅验证器
 *
 *
 * @author 李大牛家的牛 (scafel@163.com)
 * @product serverless
 * @className SubscribeValidatorHandler
 * @createDate 2025/2/8 - 16:38
 * @description 订阅验证器
 **/
@Slf4j
@Configuration(proxyBeanMethods = false)
public class SubscribeValidatorHandler implements IMqttServerSubscribeValidator {

    @Resource
    private ApplicationContext applicationContext;

    @Override
    public boolean isValid(ChannelContext context, String clientId, String topicFilter, MqttQoS qoS) {
        // 校验客户端订阅的 topic，校验成功返回 true，失败返回 false
        log.info("订阅验证器：context => {}", context);
        log.info("订阅验证器：clientId => {}", clientId);
        log.info("订阅验证器：topicFilter => {}", topicFilter);
        log.info("订阅验证器：qoS => {}", qoS);
        MqttCovert covert = applicationContext.getBean(MqttCovert.class);
        return covert.checkCanSubTopic(clientId, topicFilter);
    }
}
